﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Business;
using System.Data;
using System.Web.Security;
public partial class admin_Search_Workorder : System.Web.UI.Page
{
    String clientid = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        //clientid = Request.QueryString["client_id"];
  
        string query_client = "select * from clients";
        CreateConn conobj2 = new CreateConn();
        DataTable cdt = conobj2.SQLConnDataTable(query_client);
        string clientName;
        //  Data.i2iPortal.ClientsDataTable
        //  Selectclient_DropDownList1.Items.Clear();


        foreach (DataRow client in cdt.Rows)
        {
            clientName = client["clientName"].ToString();
            string parent_id = client["parent_client_id"].ToString();
            if (!parent_id.Equals(""))
            {
                //int parent = Int32.Parse(client["parent_client_id"].ToString());
                int parent = Int32.Parse(parent_id);
                continue;
            }
            else if (clientName.Equals("Administrator"))
            {
                continue;
            }

            String clientid = client["client_id"].ToString();
            ListItem clienitem = new ListItem(clientName, clientid, true);
            ClientsDropDownList.Items.Add(clienitem);
        }


        int clientid2 = Int32.Parse(ClientsDropDownList.SelectedValue);
        DataAccess.i2iPortalTableAdapters.SitesTableAdapter STA = new DataAccess.i2iPortalTableAdapters.SitesTableAdapter();
        string query_areas = "select * FROM clients where parent_client_id='" + clientid2 + "'";
        CreateConn conobj = new CreateConn();
        DataTable dt = conobj.SQLConnDataTable(query_areas);
        if (dt != null && dt.Rows.Count > 0)
        {
            
            foreach (DataRow dr in dt.Rows)
            {
                string area_name = dr["clientName"].ToString();
                string area_id = dr["client_id"].ToString();
                ListItem areitem = new ListItem(area_name, area_id, true);
                //areitem.Value = area_id;
                //areitem.Text = area_name;
                AreaDropDownList.Items.Add(areitem);



            }
        }
        else
        {
            AreaDropDownList.Items.Add(ClientsDropDownList.SelectedItem);
            //DataTable st = STA.GetDataByClientID(clientid);
            //SitesCheckBoxList.Items.Clear();

            //foreach (DataRow site in st.Rows)
            //{
            //    String siteName = site["siteName"].ToString();
            //    string siteid = site["site_id"].ToString();

            //    CheckBox site_CB = new CheckBox();
            //    site_CB.Text = siteName;
            //    site_CB.ID = siteid;
            //    //SitesCheckBoxList.Items.
            //    SitesCheckBoxList.Items.Add(siteName);

            //}
        }

        System.Collections.IEnumerator sitesitems = AreaDropDownList.Items.GetEnumerator();
        if (sitesitems.MoveNext())
        {
            ListItem allsitesitem = new ListItem();
            allsitesitem.Text = "SELECT ALL SITES";
            Sites_DropDownList.Items.Add(allsitesitem);
            ListItem siteitem = (ListItem)sitesitems.Current;
            int sites_clientID = Int32.Parse((siteitem.Value));
            DataTable st = STA.GetDataByClientID(sites_clientID);
            foreach (DataRow site in st.Rows)
            {
                ListItem thissite = new ListItem();
                String siteName = site["siteName"].ToString();
                string siteid = site["site_id"].ToString();
                thissite.Text = siteName;
                thissite.Value = siteid;
                //CheckBox site_CB = new CheckBox();
                //site_CB.Text = siteName;
                //site_CB.ID = siteid;
                //SitesCheckBoxList.Items.
                Sites_DropDownList.Items.Add(thissite);
            }
        }
        
 
    }
    protected void searchbuttonClicked(object sender, EventArgs e)
    {
        string workorderno = workorderTextbox.Text;
        string startdate = startdate_textbox.Text;
        string enddate = enddate_textbox.Text;
        //string completed = statusCheckbox.Checked?"1":"0";
        string completed = status_dropdownlist.SelectedValue;

        clientid = AreaDropDownList.SelectedValue;
        if (workorderno.Equals(""))
        {
            if (Sites_DropDownList.SelectedIndex == 0)
            {
                if (startdate == "" || enddate == "")
                {
                    if (completed == "2")
                    {
                        CreateConn obj = new CreateConn();
                        string query = "select workorder_id, workorder_reference_no,date_submitted,machine_id,status"
                  + " FROM WorkOrders WHERE  machine_id in (SELECT machine_id FROM Machines WHERE site_id in (SELECT site_id FROM Sites WHERE client_id='" + Int32.Parse(clientid) + "'))";
                        DataTable dt = obj.SQLConnDataTable(query);
                        Session["searchresult"] = dt;
                    }
                    else
                    {
                        CreateConn obj = new CreateConn();
                        string query = "select workorder_id, workorder_reference_no,date_submitted,machine_id,status"
               + " FROM WorkOrders WHERE status='" + completed.ToString() + "' AND machine_id in (SELECT machine_id FROM Machines WHERE site_id in (SELECT site_id FROM Sites WHERE client_id='" + Int32.Parse(clientid) + "'))";
                        DataTable dt = obj.SQLConnDataTable(query);
                        Session["searchresult"] = dt;
                    }
                }
                else
                {
                    if (completed == "2")
                    {
                        CreateConn obj = new CreateConn();
                        string query = "select workorder_id, workorder_reference_no,date_submitted,machine_id,status"
                    + " FROM WorkOrders WHERE  date_submitted BETWEEN convert(datetime,'" + startdate + "', 103)"
                    + "and convert(datetime,'" + enddate + "', 103) AND machine_id in (SELECT machine_id FROM Machines WHERE site_id in (SELECT site_id FROM Sites WHERE client_id='" + Int32.Parse(clientid) + "'))";
                        DataTable dt = obj.SQLConnDataTable(query);
                        Session["searchresult"] = dt;
                    }
                    else
                    {
                        CreateConn obj = new CreateConn();
                        string query = "select workorder_id, workorder_reference_no,date_submitted,machine_id,status"
                    + " FROM WorkOrders WHERE status='" + completed.ToString() + "' AND  date_submitted BETWEEN convert(datetime,'" + startdate + "', 103)"
                    + "and convert(datetime,'" + enddate + "', 103) AND machine_id in (SELECT machine_id FROM Machines WHERE site_id in (SELECT site_id FROM Sites WHERE client_id='" + Int32.Parse(clientid) + "'))";
                        DataTable dt = obj.SQLConnDataTable(query);
                        Session["searchresult"] = dt;
                    }

                }
            }
            else
            {
                if (startdate == "" || enddate == "")
                {
                    if (completed == "2")
                    {
                        int selectedsite = Int32.Parse(Sites_DropDownList.SelectedValue);
                        CreateConn obj = new CreateConn();
                        string query = "select workorder_id, workorder_reference_no,date_submitted,machine_id,status"
                            + " FROM WorkOrders WHERE machine_id in (SELECT machine_id FROM Machines WHERE site_id='" + selectedsite + "')";
                        DataTable dt = obj.SQLConnDataTable(query);
                        Session["searchresult"] = dt;
                    }
                    else
                    {
                        int selectedsite = Int32.Parse(Sites_DropDownList.SelectedValue);
                        CreateConn obj = new CreateConn();
                        string query = "select workorder_id, workorder_reference_no,date_submitted,machine_id,status"
                            + " FROM WorkOrders WHERE status='" + completed.ToString() + "' AND machine_id in (SELECT machine_id FROM Machines WHERE site_id='" + selectedsite + "')";
                        DataTable dt = obj.SQLConnDataTable(query);
                        Session["searchresult"] = dt;
                    }
                }
                else
                {
                    if (completed == "2")
                    {
                        int selectedsite = Int32.Parse(Sites_DropDownList.SelectedValue);
                        CreateConn obj = new CreateConn();
                        string query = "select workorder_id, workorder_reference_no,date_submitted,machine_id,status"
                            + " FROM WorkOrders WHERE  date_submitted BETWEEN convert(datetime,'" + startdate + "', 103)"
                            + "and convert(datetime,'" + enddate + "', 103) AND machine_id in (SELECT machine_id FROM Machines WHERE site_id='" + selectedsite + "')";
                        DataTable dt = obj.SQLConnDataTable(query);
                        Session["searchresult"] = dt;
                    }
                    else
                    {
                        int selectedsite = Int32.Parse(Sites_DropDownList.SelectedValue);
                        CreateConn obj = new CreateConn();
                        string query = "select workorder_id, workorder_reference_no,date_submitted,machine_id,status"
                            + " FROM WorkOrders WHERE status='" + completed.ToString() + "' AND  date_submitted BETWEEN convert(datetime,'" + startdate + "', 103)"
                            + "and convert(datetime,'" + enddate + "', 103) AND machine_id in (SELECT machine_id FROM Machines WHERE site_id='" + selectedsite + "')";
                        DataTable dt = obj.SQLConnDataTable(query);
                        Session["searchresult"] = dt;
                    }
                }

            }
        }
        else
        {
            CreateConn obj = new CreateConn();
            string query = "select workorder_id, workorder_reference_no,date_submitted,machine_id,status"
                + " FROM WorkOrders WHERE status='" + completed.ToString() + "' AND workorder_reference_no='"
                + workorderno + "'";
            DataTable dt = obj.SQLConnDataTable(query);
            Session["searchresult"] = dt;
        }

        Response.Redirect("SearchResults.aspx");
    }
    protected void backbuttonclicked(object sender, EventArgs e)
    {
        Server.Transfer("Mng_WO.aspx");
    }
    protected void ClientsDropDownList_SelectedIndexChanged(object sender, EventArgs e)
    {

        int clientid = Int32.Parse(ClientsDropDownList.SelectedValue);
        Sites_DropDownList.Items.Clear();
        AreaDropDownList.Items.Clear();
        
        DataAccess.i2iPortalTableAdapters.SitesTableAdapter STA = new DataAccess.i2iPortalTableAdapters.SitesTableAdapter();
        string query_areas = "select * FROM clients where parent_client_id='" + clientid + "'";
        CreateConn conobj = new CreateConn();
        DataTable dt = conobj.SQLConnDataTable(query_areas);
        if (dt != null && dt.Rows.Count > 0)
        {
            AreaDropDownList.Items.Clear();
            foreach (DataRow dr in dt.Rows)
            {
                string area_name = dr["clientName"].ToString();
                string area_id = dr["client_id"].ToString();
                ListItem areitem = new ListItem(area_name, area_id, true);
                //areitem.Value = area_id;
                //areitem.Text = area_name;
                AreaDropDownList.Items.Add(areitem);



            }
        }
        else
        {
            AreaDropDownList.Items.Add(ClientsDropDownList.SelectedItem);

        }
        System.Collections.IEnumerator sitesitems = AreaDropDownList.Items.GetEnumerator();
        if (sitesitems.MoveNext())
        {
            ListItem allsitesitem = new ListItem();
            allsitesitem.Text = "SELECT ALL SITES";
            Sites_DropDownList.Items.Add(allsitesitem);

            ListItem siteitem = (ListItem)sitesitems.Current;
            int sites_clientID = Int32.Parse((siteitem.Value));
            DataTable st = STA.GetDataByClientID(sites_clientID);
            foreach (DataRow site in st.Rows)
            {
                ListItem thissite = new ListItem();
                String siteName = site["siteName"].ToString();
                string siteid = site["site_id"].ToString();
                thissite.Text = siteName;
                thissite.Value = siteid;
                //CheckBox site_CB = new CheckBox();
                //site_CB.Text = siteName;
                //site_CB.ID = siteid;
                //SitesCheckBoxList.Items.
                Sites_DropDownList.Items.Add(thissite);
            }
        }
        else
        {
            ListItem allsitesitem = new ListItem();
            allsitesitem.Text = "N/A";
            allsitesitem.Value = "NA";
            Sites_DropDownList.Items.Add(allsitesitem);
           
        }
        
     
    }
    protected void AreaDropDownList_SelectedIndexChanged(object sender, EventArgs e)
    {
        int clientid = Int32.Parse(AreaDropDownList.SelectedValue);
        DataAccess.i2iPortalTableAdapters.SitesTableAdapter STA = new DataAccess.i2iPortalTableAdapters.SitesTableAdapter();

        DataTable st = STA.GetDataByClientID(clientid);
        Sites_DropDownList.Items.Clear();
        ListItem allsitesitem = new ListItem();
        allsitesitem.Text = "SELECT ALL SITES";
        Sites_DropDownList.Items.Add(allsitesitem);

        foreach (DataRow site in st.Rows)
        {
            String siteName = site["siteName"].ToString();
            string siteid = site["site_id"].ToString();

            CheckBox site_CB = new CheckBox();
            site_CB.Text = siteName;
            site_CB.ID = siteid;
            //SitesCheckBoxList.Items.
            Sites_DropDownList.Items.Add(siteName);

        }
    }
    protected void Sites_DropDownList_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void LoginStatus1_LoggingOut(object sender, LoginCancelEventArgs e)
    {


        Session.Contents.Remove("UserDetails");
        Session.Abandon();
        Session.Contents.RemoveAll();
        FormsAuthentication.SignOut();
        Response.Redirect("../Default.aspx");
    }

}
